<?php
/**
 * Created by PhpStorm.
 * User: LuLingFeng
 * Date: 2017/8/7
 * Time: 10:21
 */

namespace App\Http\Admin\App\Models;


use Framework\BaseClass\Http\Model;
use App\Eloquent\Ygt\AppVersion as EloquentVersion;

class Version extends Model
{
    /**
     * 获取APP版本信息列表
     * @Author sxy
     * @param $page
     * @param $pageSize
     * @param $searchField
     * @return mixed
     */
    public function getVersionList($page, $pageSize, $searchField)
    {
        $list = EloquentVersion::where($searchField)
            ->orderBy('is_current_version', 'desc')
            ->orderBy('updated_at', 'desc');
        $count = $list->count();
        $list = $list->forPage($page, $pageSize)->get();

        $data['list'] = $list;
        $data['pageCurrent'] = $page;
        $data['totalRow'] = $count;
        return $data;
    }

    /**
     * 获取APP版本信息详情
     * @Author sxy
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model|null|static
     */
    public function getVersionInfo($id)
    {
        $result = EloquentVersion::where('id', '=', $id)->first();
        if (is_null($result)) xThrow(ERR_APP_VERSION_NOT_EXIST);
        $result['is_force_update']==1?$result['is_force_update']="是":$result['is_force_update']="否";
        $result['is_current_version']==1?$result['is_current_version']="是":$result['is_current_version']="否";
        if($result['platform']==0){
            $result['platform']="未知";
        }elseif ($result['platform']==1){
            $result['platform']="android";
        }elseif ($result['platform']==2){
            $result['platform']="ios";
        }

        return $result;
    }

    /**
     * 编辑APP版本信息
     * @Author sxy
     * @param $request
     * @return bool|void
     */
    public function editVersion($request){
        $version = EloquentVersion::where('id', $request['id'])->first();
        if (!$version) {
            return xThrow(ERR_APP_VERSION_NOT_EXIST);
        }
        $input = [
            'name' => $request['name'],
            'version' => $request['version'],
            'url' => $request['url'],
            'is_force_update' => $request['is_force_update'],
            'is_current_version' => $request['is_current_version'],
            'platform' => $request['platform'],
            'description' => $request['description'],
            'updated_at'=>time()
        ];

        $result = $version->update($input);

        if ($input['is_current_version']) $this->resetOtherCurrentVersion($version['id'], $input['platform']);

        if ($result) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 获取编辑APP版本信息
     * @Author sxy
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model|null|static
     */
    public function editVersionInfo($id)
    {
        $result = EloquentVersion::where('id', '=', $id)->first();
        if (is_null($result)) xThrow(ERR_APP_VERSION_NOT_EXIST);


        return $result;
    }

    public function addVersion($data)
    {
        $model = new EloquentVersion($data);
        $model->save();
        if ($data['is_current_version']) $this->resetOtherCurrentVersion($model['id'], $data['platform']);
        return $model;
    }

    private function resetOtherCurrentVersion($exceptId, $platform)
    {
        EloquentVersion::with([])
            ->where('id', '!=', $exceptId)
            ->where([
                'platform'           => $platform,
                'is_current_version' => 1,
            ])
            ->update([
                'is_current_version' => 0,
            ]);
    }
}